package com.wobangkj.common.repository;

import com.wobangkj.common.model.crud.Liked;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface LikedRepository extends JpaRepository<Liked, Long> {
    @Query(nativeQuery = true, value="select * from  liked  where liker_id=:likerId and liked_content_id=:likedContentId and liked_class=:likedClass ")
    List<Liked> findById(String likerId, String likedContentId, String likedClass);

    @Query(nativeQuery = true, value="select l.* ,u.name,u.headimg,u.is_master from  liked l join user u on l.liker_id=u.id  " +
            "where l.liked_content_publisher_id=:Id ORDER BY l.createtime DESC ")
    List<Map> findMy(String Id);

    @Query(nativeQuery = true, value="update share set share_like_num=share_like_num+1 where id=:id  ")
    @Modifying
    public void updateShareNumById(Long id);

    @Query(nativeQuery = true, value="update dynamic set dynamic_like_num=dynamic_like_num+1 where id=:id  ")
    @Modifying
    public void updateDynamicNumById(Long id);

    @Query(nativeQuery = true, value="update comment set comment_like_num=comment_like_num+1 where id=:id  ")
    @Modifying
    public void updateCommentNumById(Long id);

    @Query(nativeQuery = true, value="update share set share_like_num=share_like_num-1 where share.id in (select liked_content_id from liked WHERE liked.id = :id) ")
    @Modifying
    public void deleteShareNumById(Long id);

    @Query(nativeQuery = true, value="update dynamic set dynamic_like_num=dynamic_like_num-1 where dynamic.id in (select liked_content_id from liked WHERE liked.id = :id)  ")
    @Modifying
    public void deleteDynamicNumById(Long id);

    @Query(nativeQuery = true, value="update comment set comment_like_num=comment_like_num-1 where comment.id in (select liked_content_id from liked WHERE liked.id = :id)  ")
    @Modifying
    public void deleteCommentNumById(Long id);

    @Query(nativeQuery = true, value="select id from  liked  where liker_id=:likerId and liked_content_id=:likedContentId and liked_class=:likedClass ")
    public Long findLiked(String likerId, String likedContentId ,String likedClass);

    @Query(nativeQuery = true, value="select share_id from  share  where id=:ContentId ")
    public String getSharerId(String ContentId );

    @Query(nativeQuery = true, value="select dynamic_publisher_id from  dynamic  where id=:ContentId ")
    public String getDynamicPublisherId(String ContentId );

    @Query(nativeQuery = true, value="select comment_publisher_id from  comment  where id=:ContentId ")
    public String getCommentPublisherId(String ContentId );
}
